package advanced.thread.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;


/**
 * 任务调度框架 quartz    入门学习
 */
public class L_Quartz implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) {
        System.out.println("--start---");
        System.out.println("hello world" + new Date());
        System.out.println("---end---");
    }

    public L_Quartz() {

    }

    public static void main(String[] args) {
        //1. 创建scheduler 工厂
        StdSchedulerFactory sf = new StdSchedulerFactory();
        //2. 从工厂中获取调度器
        Scheduler scheduler = null;
        try {
            scheduler = sf.getScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        //3. 创建 JobDetail
        JobDetail job = JobBuilder.newJob(L_Quartz.class).withIdentity("job1", "group1").build();
        //时间
        Date runTimie = DateBuilder.evenMinuteDate(new Date());
        //4 触发器
        Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "group1").startAt(runTimie).build();

        //告诉quartz 使用触发器调度任务
        try {
            scheduler.scheduleJob(trigger);
            //启动
            scheduler.start();
            //停止
            scheduler.shutdown();

        } catch (SchedulerException e) {
            e.printStackTrace();
        }


    }

}
